{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date\n",
    "from gs_quant.instrument import EqOption, OptionType, OptionStyle, UnderlierType\n",
    "from gs_quant.session import Environment, GsSession"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a .STOXX50E 3m call option striking at-the-money spot\n",
    "eq_option = EqOption('.STOXX50E', expiration_date='3m', strike_price='ATMS', option_type=OptionType.Call,\n",
    "                     option_style=OptionStyle.European)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculate local price and dollar price\n",
    "print('Local price:      {:,.4f}'.format(eq_option.price()))\n",
    "print('Dollar price:     {:,.4f}'.format(eq_option.dollar_price()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Underlier Syntax\n",
    "\n",
    "The underlier accepts an underlier as a RIC or BBID identifier. The default is RIC.\n",
    "\n",
    "| Syntax  | Defintion           |\n",
    "|---------|---------------------|\n",
    "|  'RIC'  | Reuters identifier      |\n",
    "|  'BBID'  | Bloomberg identifier        |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# resolve using a Bloomberg ID\n",
    "eq_option_bbid = EqOption('SX5E', underlier_type=UnderlierType.BBID, expiration_date='3m', strike_price='ATMS', option_type=OptionType.Call,\n",
    "                     option_style=OptionStyle.European)\n",
    "\n",
    "eq_option_bbid.resolve()\n",
    "eq_option_bbid.as_dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Strike Syntax\n",
    "The strike_price syntax allows for an int or a string. The absolute level can be specified using an integer.\n",
    "\n",
    "The following solver keys using a string format are accepted: \n",
    "\n",
    "| Syntax  | Defintion           |\n",
    "|---------|---------------------|\n",
    "|  '%'   | Percent of Spot      |\n",
    "|  'ATMS'  | At the Money        |\n",
    "|  'ATMF' | At the Money Forward|\n",
    "|  'D'    | Delta Strikes       |\n",
    "|  'P'    | Premium             |\n",
    "\n",
    "    - For ATM, ATMF: '1.05*ATMF+.01'\n",
    "    - For Delta Strikes, specify the option delta: '25D', '20D-.01', etc.\n",
    "    - You can also solve for Premium: P=,<target>% "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# resolve with strike at 110% of spot\n",
    "eq_atm_solver = EqOption('.STOXX50E', expiration_date='3m', strike_price='ATMS+10%', option_type=OptionType.Put,\n",
    "                     option_style=OptionStyle.European)\n",
    "\n",
    "eq_atm_solver.resolve()\n",
    "eq_atm_solver.strike_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# resolve with strike at 94.5% of spot\n",
    "eq_spot_pct = EqOption('.STOXX50E', expiration_date='3m', strike_price='94.5%', option_type=OptionType.Put,\n",
    "                     option_style=OptionStyle.European)\n",
    "\n",
    "eq_spot_pct.resolve()\n",
    "eq_spot_pct.strike_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# resolve with strike at spot minus 10\n",
    "eq_atmf_solver = EqOption('.STOXX50E', expiration_date='1m', strike_price='ATMF-10', option_type=OptionType.Put,\n",
    "                     option_style=OptionStyle.European)\n",
    "\n",
    "eq_atmf_solver.resolve()\n",
    "eq_atmf_solver.strike_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# resolve with strike solving for 10% premium \n",
    "eq_10x = EqOption('.STOXX50E', expiration_date='6m', strike_price='P=10%', option_type=OptionType.Put,\n",
    "                     option_style=OptionStyle.European)\n",
    "\n",
    "eq_10x.resolve()\n",
    "eq_10x.strike_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# resolve with strike solving for absolute premium \n",
    "# 'PriceLocal' and 'PriceUSD' are also valid\n",
    "eq_abs_prem = EqOption('.STOXX50E', expiration_date='6m', strike_price='Price=1000', option_type=OptionType.Put,\n",
    "                     option_style=OptionStyle.European)\n",
    "\n",
    "eq_abs_prem.resolve()\n",
    "print(eq_abs_prem.price())\n",
    "eq_abs_prem.strike_price"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
